﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Globalization;

namespace University.Workshop.DataAccess
{
    /// <summary>
    /// This Class Was Made By Marieta for exclusive use in CheckInForm at WinApp and  ClientManager
    /// </summary>
    public class ClientDataAccess :IDisposable
    {
        public const string ConnString = @"Data Source=.\SQLEXPRESS;Initial Catalog=DataBaseHotel;Integrated Security=True";
        private SqlConnection cnn;
        public string GetFullNameFormCI(int clientId)
        {
            string fullName="";

            cnn = new SqlConnection(ConnString);
            string sqlSentence = "select * from  [DataBaseHotel].[dbo].[Client] WHERE ClientId=@id";

            SqlCommand cmd = cnn.CreateCommand();

            cmd.CommandText = sqlSentence;
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@id",Convert.ToString(clientId,CultureInfo.InvariantCulture));
            try
            {
                cnn.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        fullName = reader["FullName"].ToString();    
                    }
                }
            }
            catch (Exception e)
            {
                throw new ArgumentException("Error Getting a Full Name", e);

            }
            finally
            {
                if (cnn.State == System.Data.ConnectionState.Open)
                {
                    cnn.Close();
                }
            }
            return fullName;
        }

        #region Miembros de IDisposable

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (cnn != null)
                {
                    cnn.Dispose();
                    cnn = null;
                }
            }
        }

        #endregion
    }
}
